# coding:utf-8
from django.contrib import admin
from primary_blog.models import Article, Category, Carousel, Nav, Column, News, Blogroll, Tag
import datetime


class CategoryAdmin(admin.ModelAdmin):
    """
    分类
    """
    search_fields = ('name',)
    list_filter = ('status', 'create_time')
    list_display = ('name', 'parent', 'rank', 'status')
    fields = ('name', 'parent', 'rank', 'status')


class ArticleAdmin(admin.ModelAdmin):
    """
    文章
    """
    search_fields = ('title', 'summary')
    list_filter = ('status', 'category', 'is_top', 'create_time', 'update_time', 'is_top')
    list_display = ('title', 'category', 'author', 'status', 'is_top', 'update_time')
    fieldsets = (
        (u'基本信息', {
            'fields': ('title', 'img', 'en_title', 'category', 'tags', 'author', 'is_top', 'rank', 'status')
        }),
        (u'内容', {
            'fields': ('content',)
        }),
        (u'摘要', {
            'fields': ('summary',)
        }),
        (u'时间', {
            'fields': ('pub_time',)
        }),
    )

    def save_model(self, request, obj, form, change):
        """
        覆盖添加文章的方法,在添加文章时保存关联的标签
        :param request:
        :param obj:
        :param form:
        :param change:
        :return:
        """
        tags = obj.tags
        if tags:
            if u',' in tags or u'，' in tags:
                tag_list = tags.replace(u'，', ',').replace(' ', '').split(',')
                if tag_list:
                    for tag_name in tag_list:
                        updated_values = {'name': tag_name, 'desc': u"添加文章%s是添加的标签" % obj.title, 'create_date': datetime.datetime.now()}
                        Tag.objects.get_or_create(name__iexact=tag_name, defaults=updated_values)
            else:
                updated_values = {'name': tags, 'desc': u"添加文章%s是添加的标签" % obj.title, 'create_date': datetime.datetime.now()}
                Tag.objects.get_or_create(name__iexact=tags, defaults=updated_values)
        obj.save()


class NewsAdmin(admin.ModelAdmin):
    """
    新闻
    """
    search_fields = ('title', 'summary')
    list_filter = ('news_from', 'create_time')
    list_display = ('title', 'news_from', 'url', 'create_time')
    fields = ('title', 'news_from', 'url', 'summary', 'pub_time')


class NavAdmin(admin.ModelAdmin):
    """
    导航
    """
    search_fields = ('name',)
    list_display = ('name', 'url', 'status', 'create_time')
    list_filter = ('status', 'create_time')
    fields = ('name', 'url', 'status')


class ColumnAdmin(admin.ModelAdmin):
    """
    专栏
    """
    search_fields = ('name',)
    list_display = ('name', 'status', 'create_time')
    list_filter = ('status', 'create_time')
    fields = ('name', 'status', 'article', 'summary')
    filter_horizontal = ('article',)


class CarouselAdmin(admin.ModelAdmin):
    """
    轮播
    """
    search_fields = ('title',)
    list_display = ('title', 'article', 'img', 'create_time')
    list_filter = ('create_time',)
    fields = ('title', 'article', 'img', 'summary')


class BlogrollAdmin(admin.ModelAdmin):
    """
    友情链接
    """
    search_fields = ('name',)
    list_display = ('name', 'pub_date')
    list_filter = ('pub_date',)

    fields = ('name', 'pub_date', 'target_url')


class TagAdmin(admin.ModelAdmin):
    """
    标签
    """
    search_fields = ('name',)
    list_display = ('name', 'desc', 'create_date')
    list_filter = ('create_date',)

    fields = ('name', 'desc', 'create_date')


admin.site.register(Category, CategoryAdmin)
admin.site.register(Article, ArticleAdmin)
admin.site.register(News, NewsAdmin)
admin.site.register(Nav, NavAdmin)
admin.site.register(Column, ColumnAdmin)
admin.site.register(Carousel, CarouselAdmin)
admin.site.register(Blogroll, BlogrollAdmin)
admin.site.register(Tag, TagAdmin)
